/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototype.runtime.federate.interactions;

import hla.rti.ArrayIndexOutOfBounds;
import hla.rti.RTIinternalError;
import hla.rti.ReceivedInteraction;
import hla.rti.SuppliedParameters;
import hla.rti.jlc.EncodingHelpers;
import hla.rti.jlc.RtiFactoryFactory;
import java.util.List;
import prototype.persistence.pojo.Scenario;
import prototype.persistence.pojo.User;
import prototype.persistence.pojo.UserHasUnit;
import prototype.runtime.interfaces.RuntimeUnit;

/**
 *
 * @author Server
 */
public class Death {

    int id;

    public Death(int id) {
        this.id = id;
    }

    public Death(ReceivedInteraction theInteraction) throws ArrayIndexOutOfBounds {
        id = EncodingHelpers.decodeInt(theInteraction.getValue(0));
    }

    public SuppliedParameters getParameters(int idH) throws RTIinternalError {
        SuppliedParameters parameters =
                RtiFactoryFactory.getRtiFactory().
                createSuppliedParameters();
        parameters.add(idH, EncodingHelpers.encodeInt(id));
        return parameters;
    }

    public void apply(Scenario scenario) {
        for (User u : scenario.getUsers()) {
            for (UserHasUnit uhu : u.getUserHasUnits()) {
                if (uhu.getId().equals(id)) {
                    uhu.getUnit().setHealth(-10);
                    System.out.println("Removed unit " + uhu.getUnit().getName());
                    return;
                }
            }
        }
    }

    public int getId() {
        return id;
    }
    
}
